package com.sunlee.admin.api.module.job.task;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sunlee.admin.api.module.job.dto.MarketingProductTaskDto;
import com.sunlee.common.core.exception.BusinessException;
import com.sunlee.common.service.entity.MarketingEntity;
import com.sunlee.common.service.entity.MarketingProductMappingEntity;
import com.sunlee.common.service.entity.ProductEntity;
import com.sunlee.common.service.service.MarketingProductMappingService;
import com.sunlee.common.service.service.MarketingService;
import com.sunlee.common.service.service.ProductService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 首页和分类营销资源推荐任务
 *
 * @author sunlee
 */
@Component("marketingProductTask")
public class MarketingProductTask implements ITask {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private MarketingService marketingService;

    @Autowired
    private MarketingProductMappingService marketingProductMappingService;

    @Autowired
    private ProductService productService;

    @Override
    public void run(String params) {
        MarketingProductTaskDto dto;
        try {
            dto = JSON.parseObject(params, MarketingProductTaskDto.class);
            if (StringUtils.isBlank(dto.getName())
                    || Objects.isNull(dto.getFilterMultiImg())
                    || Objects.isNull(dto.getNum())
                    || dto.getNum() <= 0) {
                throw new BusinessException("缺少参数");
            }
        } catch (Exception e) {
            throw new BusinessException("参数解析异常");
        }
        updateMarketingProductMapping(dto);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateMarketingProductMapping(MarketingProductTaskDto dto) {
        // 查询营销类型
        MarketingEntity marketingEntity = marketingService.getOneOwn(new LambdaQueryWrapper<MarketingEntity>().eq(MarketingEntity::getName, dto.getName()));
        Long mId = marketingEntity.getId();
        // 查询随机资源
        List<ProductEntity> productList = productService.getAny(
                new LambdaQueryWrapper<ProductEntity>().isNotNull(dto.getFilterMultiImg(), ProductEntity::getMultiImg)
                , dto.getNum());

        // 封装新关联资源
        List<MarketingProductMappingEntity> newList = new ArrayList<>();
        Date nowDate = new Date();
        for (int i = 0; i < productList.size(); i++) {
            MarketingProductMappingEntity newMappingEntity = new MarketingProductMappingEntity();
            newMappingEntity.setProductId(productList.get(i).getId());
            newMappingEntity.setMarketingId(mId);
            newMappingEntity.setCreator(0L);
            newMappingEntity.setCreateDate(nowDate);
            newMappingEntity.setUpdateDate(nowDate);
            newMappingEntity.setSort(i);
            newList.add(newMappingEntity);
        }
        // 删除关联资源
        marketingProductMappingService.deleteByMIds(Collections.singletonList(mId));
        // 插入关联资源
        marketingProductMappingService.insertBatchOwn(newList);
    }

}
